﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Business.WPF;
using DataAccess.Exceptions;
using Model;
using Presenters.WPF.Views;

namespace Presenters.WPF
{
    public class NewStudentPresenter
    {
        public INewStudentView View { get; set; }

        public NewStudentPresenter(INewStudentView view)
        {
            this.View = view;
        }
        public bool ValidateIDText()
        {
            try
            {
                Business.Tools.ValidationText.isNumber(this.View.id.ToString(), "ID");
                return (true);
            }
            catch (Exception ex)
            {
                this.View.message = ex.Message;
            }
            return (false);
        }
        public bool ValidateTelephoneText()
        {
            try
            {
                Business.Tools.ValidationText.isNumber(this.View.telephone.ToString(), "Telephone");
                return (true);
            }
            catch (Exception ex)
            {
                this.View.message = ex.Message;
            }
            return (false);
        }
        public bool ValidateLiceAeroText()
        {
            try
            {
                Business.Tools.ValidationText.isNumber(this.View.liceAero.ToString(), "Lice Aero");
                return (true);
            }
            catch (Exception ex)
            {
                this.View.message = ex.Message;
            }
            return (false);
        }
        public void SubmitNewStudent()
        {
            try
            {
                if (String.IsNullOrEmpty(View.lastname) || String.IsNullOrEmpty(View.mlastname) ||
                    String.IsNullOrEmpty(View.name) || String.IsNullOrEmpty(View.sex) ||
                    String.IsNullOrEmpty(View.profession) || String.IsNullOrEmpty(View.birthplace) ||
                    String.IsNullOrEmpty(View.address) || String.IsNullOrEmpty(View.zone) ||
                    String.IsNullOrEmpty(View.telephone) || string.IsNullOrEmpty(View.email) ||
                    String.IsNullOrEmpty(View.fName) || String.IsNullOrEmpty(View.fProfession) ||
                    String.IsNullOrEmpty(View.mName) || String.IsNullOrEmpty(View.mProfession) ||
                    String.IsNullOrEmpty(View.observations) || String.IsNullOrEmpty(View.issuedIn) ||
                    String.IsNullOrEmpty(View.liceAero))
                    throw new ValidationException("Llenar todos los campos");

                //Add if birthdate is null and the same for ID
                //else
                //{
                string emailPattern = @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                             @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";

                if (!Regex.IsMatch(View.email, emailPattern))
                    throw new BusinessException("El email es incorrecto!");

                Student student = new Student();
                NewStudentRegistrationManager nsm = new NewStudentRegistrationManager();
                if (nsm.StudentAlreadyRegistered(View.id) == true)
                    throw new DataAccessException("El estudiante ya esta registrado!");

                student = nsm.NewStudent(View.name, View.lastname, View.mlastname, View.sex,
                              View.birthplace, View.birthday, View.issuedIn, View.liceAero,
                              View.profession, View.address, View.zone, View.telephone, View.email,
                              View.id, View.fName, View.fProfession, View.mName, View.mProfession, View.observations);

                //}

            }
            catch (ValidationException)
            {
                throw new ValidationException("Llenar todos los campos.");
            }
            catch (BusinessException)
            {
                throw new BusinessException("El email es incorrecto asegurese de que este bien escrito!");
            }
            catch (DataAccessException)
            {
                throw new DataAccessException("El estudiante ya esta registrado! Revise el campo CI");
            }
        }
        public void SubmitModifyStudent()
        {
            try
            {
                if (String.IsNullOrEmpty(View.profession) || String.IsNullOrEmpty(View.address) ||
                    String.IsNullOrEmpty(View.zone) || String.IsNullOrEmpty(View.telephone) ||
                    String.IsNullOrEmpty(View.email) || String.IsNullOrEmpty(View.fProfession) ||
                    String.IsNullOrEmpty(View.mProfession) || String.IsNullOrEmpty(View.observations) ||
                    String.IsNullOrEmpty(View.issuedIn) || String.IsNullOrEmpty(View.liceAero))
                    throw new ValidationException("Llenar todos los campos");
                //Add if birthdate is null and the same for ID


                string emailPattern = @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                         @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";

                if (!Regex.IsMatch(View.email, emailPattern))
                    throw new BusinessException("El email es incorrecto!");
                //else
                //{
                Student student = new Student();
                NewStudentRegistrationManager nsm = new NewStudentRegistrationManager();
               
                student = nsm.ModifyStudent(View.name, View.lastname, View.mlastname, View.sex,
                                  View.birthplace, View.birthday, View.issuedIn, View.liceAero,
                                  View.profession, View.address, View.zone, View.telephone, View.email,
                                  View.id, View.fName, View.fProfession, View.mName, View.mProfession, View.observations);
                //}


            }
            catch (ValidationException)
            {
                throw new ValidationException("Llenar todos los campos.");
            }
            catch (BusinessException)
            {
                throw new BusinessException("El email es incorrecto asegurese de que este bien escrito!");
            }
            
        }
        private void NewStudent()
        {
 
        }
    }
}
